package com.atguigu.java;

import org.junit.Test;

import java.io.*;

public class ObjectTest {
    @Test
    public void test() throws IOException, ClassNotFoundException {
        ObjectInputStream ois=null;
        try {
            ois = new ObjectInputStream(new FileInputStream("object.dat"));
            Object obj = ois.readObject();
            String str = (String)obj;
            Person p = (Person) ois.readObject();
            System.out.println(str);
            System.out.println(p);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(ois!=null)
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }


    }
    @Test
    public void test1() throws IOException {
        ObjectOutputStream oos=null;
        try {
             oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
            oos.writeObject(new String("林东松我爱你"));
            oos.flush();
            oos.writeObject(new Person("林东松",20));
            oos.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(oos!=null)
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }


    }
}
